/**
 * @program: clms-second
 * @description: 用户控制层
 * @author: mitu_echo
 * @create: 2021-07-22 15:53
 **/

package com.second.system.controller;

import com.second.common.pojo.R;
import com.second.system.config.RsaKeyProperties;
import com.second.system.pojo.entity.User;
import com.second.system.pojo.utils.MyAnnotation;
import com.second.system.pojo.utils.UploadUtil;
import com.second.system.pojo.vo.LoginVo;
import com.second.system.pojo.vo.ShowAllUserVO;
import com.second.system.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RsaKeyProperties rsaKeyProperties;
    @Autowired
    private UserService userService;

    private List<String> urlList;

    @ApiOperation("登录方法")
    @RequestMapping("/login")
    public R doLogin(LoginVo loginVo) {

        return userService.doLogin(loginVo);
    }

    @ApiOperation("根据请求头的 token 获取用户信息")
    @RequestMapping("/getUserInfo")
    public R getUserInfo() {

        return userService.getUserInfo();
    }

    @ApiOperation("查询全部的用户信息")
    @RequestMapping("/showAllUser")
    public R showAllUser(ShowAllUserVO showAllUserVO) {

        return userService.showAllUser(showAllUserVO);
    }

    @ApiOperation("上传文件的接口")
    @RequestMapping("/upload")
    public Map Ipload (MultipartFile file) {
        // 将图片回显给前端
        urlList = UploadUtil.getUrl(file);
        HashMap map = new HashMap();
        if (urlList.size()>0) {
            map.put("img",urlList.get(0));
        }
        return map;
    }

    @ApiOperation("新增用户")
    @RequestMapping("/addUser")
    public R addUser(@RequestBody User user) {

        return userService.addUser(user);
    }

    @ApiOperation("删除用户")
    @RequestMapping("/delUserByUserid")
    public R delUserByUserid(Integer userid) {

        return userService.delUserByUserid(userid);
    }

    @ApiOperation("修改用户信息")
    @RequestMapping("/updateUser")
    public R updateUser(@RequestBody User user) {

        return userService.updateUser(user);
    }

    @ApiOperation("修改用户的状态")
    @RequestMapping("/updateStatus")
    public R updateStatus(Integer userid,Character status) {

        return userService.updateStatus(userid,status);
    }

    @ApiOperation("修改用户的密码")
    @RequestMapping("/updatePwd")
    public R updatePwd(String userpwd) {

        System.out.println(userpwd);
        return userService.updatePwd(userpwd);
    }
}
